package com.frxuexi.user.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.R
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.viewmodel.AskViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

/**
 * 待审批列表页面
 */
@Composable
fun ApproveListPage(navHostController: NavHostController, vm: AskViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<AskLeave>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect {
                when (it) {
                    is IUiState.onSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as Collection<AskLeave>)
                    }

                    is IUiState.onError -> {
                        Log.i("===", it.msg)
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(AskIntent.selectByNoApprove)
    })

    Column {
        MyTitleBar(navHostController = navHostController, title = "待审核")
        Column (Modifier.padding(horizontal = 20.dp)){
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(colorResource(id = R.color.lightblue))
                    .padding(vertical = 16.dp),
                horizontalArrangement = Arrangement.SpaceAround
            ) {
                Text(text = "类型")
                Text(text = "开始时间")
                Text(text = "结束时间")
                Text(text = "审批状态")
            }
            Spacer(modifier = Modifier.height(10.dp))
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list) {
                    Row(
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(
                                colorResource(id = R.color.lightgray),
                                RoundedCornerShape(20.dp)
                            )
                            .padding(vertical = 16.dp)
                            .clickable {
                                val json = Gson().toJson(it)
                                navHostController.navigate(RouterPath.APPROVE_PAGE + "/$json")
                            },
                        horizontalArrangement = Arrangement.SpaceAround,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Text(text = it.type!!)
                        Text(text = it.start!!)
                        Text(text = it.end!!)
                        Text(
                            text = it.status!!,
                            Modifier
                                .background(Color.LightGray, RoundedCornerShape(10.dp))
                                .padding(5.dp)
                        )
                    }
                }
            })
        }
    }
}